package app.modules.security.persistence.dao;

import app.modules.security.dto.web.role.WebAssignedFunctionDTO;
import app.modules.security.persistence.mapper.RoleFunctionRelationMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jasmine.framework.common.util.CollectionUtil;
import jasmine.framework.database.mybatisplus.dao.BaseEntityDAO;
import jasmine.security.rbac.mapper.SecRoleFunctionRelationMapper;
import jasmine.security.rbac.model.SecRoleFunctionRelation;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author mh.z
 */
@Repository
public class RoleFunctionRelationDAO
        extends BaseEntityDAO<RoleFunctionRelationMapper, SecRoleFunctionRelation> {

    /**
     * 查询指定角色的所有已分配功能
     *
     * @param roleId
     * @return
     */
    public List<WebAssignedFunctionDTO> listWebAssignedFunctionDTOs(Long roleId) {
        LambdaQueryWrapper<SecRoleFunctionRelation> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SecRoleFunctionRelation::getRoleId, roleId);
        wrapper.orderByAsc(SecRoleFunctionRelation::getId);

        List<SecRoleFunctionRelation> recordLit = baseMapper.selectList(wrapper);
        List<WebAssignedFunctionDTO> dtoList = CollectionUtil.mapToList(recordLit, (record) -> {
            WebAssignedFunctionDTO dto = new WebAssignedFunctionDTO();
            dto.setId(record.getFunctionId());

            return dto;
        });

        return dtoList;
    }

    /**
     * 删除指定角色分配的所有功能
     *
     * @param roleId
     */
    public void deleteByRoleId(Long roleId) {
        LambdaQueryWrapper<SecRoleFunctionRelation> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SecRoleFunctionRelation::getRoleId, roleId);

        baseMapper.delete(wrapper);
    }

    /**
     * 删除指定功能的所有角色分配分析
     *
     * @param functionId
     */
    public void deleteByFunctionId(Long functionId) {
        baseMapper.deleteByFunctionId(functionId);
    }

}
